package com.sandman.article.controller;

import com.sandman.article.service.ArticleService;
import com.sandman.enums.HttpCodeEnum;
import com.sandman.vo.ArticleVo;
import com.sandman.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author 范珈诚
 * time 10:32 PM 9/25/22
 * notes
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章服务")
public class ArticleController {

    private ArticleService articleService;

    @Autowired
    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    /**
     * 获取随机文章
     */
    @GetMapping("/getOne/{sortBy}")
    @ApiOperation("根据排序选项获取文章列表")
    public Result getOne(@PathVariable("sortBy") Byte sortBy) {
        final ArticleVo one = articleService.getOne(sortBy);
        if (one == null) {
            //世界尽头
            Result.error(HttpCodeEnum.DATA_NOT_EXIST);
        }
        return Result.sucess(one);
    }

    /**
     * 新增文章
     */
    @PostMapping
    @ApiOperation(value = "新增一篇文章")
    public Result createArticle(@RequestBody ArticleVo articleVo) {
        articleService.createArticle(articleVo);
        return Result.sucess();
    }
}
